﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;

namespace sydfdl.TMS.BLL
{
    public class BLLT_E_GoodsOrder
    {
        public DataTable GetCustomerInfoByPhone(string phonenum)
        {
            DataTable dt = new DataTable();
            try
            {
                SqlParameter[] para = 
                                    {
                                      new SqlParameter("@PhoneNum",SqlDbType.NVarChar,50)
                                    };

                para[0].Value = phonenum;

                dt = SqlHelper.ExecuteProcedureToDataSet("P_E_GoodsOrder_RetrieveCustomerInfoByPhone", para, "").Tables[0];
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }

            return dt;
        }

        public DataTable GetDestinationByCode(string code)
        {
            DataTable dt = new DataTable();
            try
            {
                SqlParameter[] para = 
                                    {
                                        new SqlParameter("@Code",SqlDbType.NVarChar,50)
                                    };
                para[0].Value = code;

                dt = SqlHelper.ExecuteProcedureToDataSet("P_E_GoodsOrder_RetrieveDestinationByCode", para, "").Tables[0];
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }

            return dt;
        }

        public DataTable GetFreightType()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = SqlHelper.ExecuteProcedureToDataSet("P_E_GoodsOrder_RetrieveFreightType", null, "").Tables[0];
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }
            return dt;
        }

        public DataTable GetReceiverGetGoodsType()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = SqlHelper.ExecuteProcedureToDataSet("P_E_GoodsOrder_RetrieveReceiverGetGoodsType", null, "").Tables[0];
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }
            return dt;
        }

        public DataTable GetAllCompany()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = SqlHelper.ExecuteProcedureToDataSet("P_E_GoodsOrder_RetrieveAllCompany", null, "").Tables[0];
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }
            return new DataTable();
        }

        public Dictionary<string, string> SaveGoodsOrderInfo(IEnumerable<T_E_GoodsOrder> orderData, IEnumerable<T_E_GoodsOrderDetail> goodgridData, string processUser)
        {
            Dictionary<string, string> msg = new Dictionary<string, string>();
            msg.Add("SaveMsg", string.Empty);
            msg.Add("OrderID", string.Empty);
            msg.Add("SenderID", string.Empty);
            msg.Add("SenderCode", string.Empty);
            msg.Add("ReceiverID", string.Empty);
            msg.Add("ReceiverCode", string.Empty);
            processUser = "00000000-0000-0000-0000-000000000000";
            try
            {
                string orderXml = EFHelp.GetXML(orderData);
                string goodgrid = EFHelp.GetXML(goodgridData);

                SqlParameter[] para =
                                    {
                                         new SqlParameter("@OrderXml",SqlDbType.Xml,-1),
                                         new SqlParameter("@GoodGrid",SqlDbType.Xml,-1),
                                         new SqlParameter("@ProcessUser",SqlDbType.NVarChar,100),
                                         new SqlParameter("@SaveMsg",SqlDbType.NVarChar,100),
                                         new SqlParameter("@OrderID",SqlDbType.NVarChar,100),
                                         new SqlParameter("@SenderID",SqlDbType.NVarChar,100),
                                         new SqlParameter("@SenderCode",SqlDbType.NVarChar,100),
                                         new SqlParameter("@ReceiverID",SqlDbType.NVarChar,100),
                                         new SqlParameter("@ReceiverCode",SqlDbType.NVarChar,100)
                                    };

                para[0].Value = orderXml;
                para[1].Value = goodgrid;
                para[2].Value = processUser;
                para[3].Direction = ParameterDirection.Output;
                para[4].Direction = ParameterDirection.Output;
                para[5].Direction = ParameterDirection.Output;
                para[6].Direction = ParameterDirection.Output;
                para[7].Direction = ParameterDirection.Output;
                para[8].Direction = ParameterDirection.Output;

                SqlHelper.ExecuteNonQueryStoredProcedure(@"P_E_GoodsOrder_SaveGoodsOrderInfo", para);

                msg["SaveMsg"] = (para[3].Value ?? string.Empty).ToString();
                msg["OrderID"] = (para[4].Value ?? string.Empty).ToString();
                msg["SenderID"] = (para[5].Value ?? string.Empty).ToString();
                msg["SenderCode"] = (para[6].Value ?? string.Empty).ToString();
                msg["ReceiverID"] = (para[7].Value ?? string.Empty).ToString();
                msg["ReceiverCode"] = (para[8].Value ?? string.Empty).ToString();
                //msg["SaveMsg"] = "test info";
            }
            catch (Exception ex)
            {
                msg["SaveMsg"] = ex.Message;
            }

            return msg;
        }
    }
}
